package org.linlinjava.litemall.mechanismadmin.web;

import com.github.pagehelper.PageInfo;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallMechanismGoodsCategory;
import org.linlinjava.litemall.db.service.LitemallMechanismGoodsCategoryService;
import org.linlinjava.litemall.mechanismadmin.annotation.LoginAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 机构商品分类
 *
 * @author zhy
 * @create 2018-12-17 14:27
 **/
@RestController
@RequestMapping("/mechanismadmin/category")
public class MechanismCategoryController {
	private final Log logger = LogFactory.getLog(MechanismCategoryController.class);
	@Autowired
	private LitemallMechanismGoodsCategoryService mechanismGoodsCategoryService;
	@GetMapping("/list")
	public Object list(@LoginAdmin Integer adminId,
		String id, String name,Integer orgId,
		@RequestParam(value = "page", defaultValue = "1") Integer page,
		@RequestParam(value = "limit", defaultValue = "10") Integer limit,
		String sort, String order){
		if(adminId == null){
			return ResponseUtil.unlogin();
		}

		List<LitemallMechanismGoodsCategory> collectList = mechanismGoodsCategoryService.querySelective(id, name,orgId, page, limit, sort, order);
		long total = PageInfo.of(collectList).getTotal();
		Map<String, Object> data = new HashMap<>();
		data.put("total", total);
		data.put("items", collectList);

		return ResponseUtil.ok(data);
	}

	@PostMapping("/create")
	public Object create(@LoginAdmin Integer adminId, @RequestBody LitemallMechanismGoodsCategory category){
		if(adminId == null){
			return ResponseUtil.unlogin();
		}
		category.setAddTime(LocalDateTime.now());
		mechanismGoodsCategoryService.add(category);
		return ResponseUtil.ok();
	}

	@PostMapping("/update")
	public Object update(@LoginAdmin Integer adminId, @RequestBody LitemallMechanismGoodsCategory category){
		if(adminId == null){
			return ResponseUtil.unlogin();
		}
		mechanismGoodsCategoryService.updateById(category);
		return ResponseUtil.ok();
	}

	@PostMapping("/delete")
	public Object delete(@LoginAdmin Integer adminId, @RequestBody LitemallMechanismGoodsCategory category){
		if(adminId == null){
			return ResponseUtil.unlogin();
		}
		mechanismGoodsCategoryService.deleteById(category.getId());
		return ResponseUtil.ok();
	}
}
